<?php

/**
 * profile actions.
 *
 * @package    travide
 * @subpackage profile
 * @author     Travide Project Group
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class profileActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->forward('default', 'module');
    }

    public function executeViewMyProfile(sfWebRequest $request) {
       // $profileUins = $this->getUser()->getProfileUin();

       // if ((count($profileUins) == 0) || (count($profileUins) > 1)) {
            /**
             * If the user doesn't have a profile or has more than one
             * display the profile management list
             */
        /**
         * Ignore previous functionality- always redirect to profile management
         */
            $this->redirect('profile/userProfileManagement?uin='.$this->getUser()->getGuardUser()->getId());
      //  } else {
            /**
             * If the user only has one profile display the profile immediately
             */
        //    $this->redirect('collection/show?uin=' . $profileUins[0]);
       // }
    }

    public function executeUserProfileManagement(sfWebRequest $request) {

        $this->forward404Unless($this->searchUser = Doctrine::getTable('sfGuardUser')->find($request->getParameter('uin')));
        $this->userProfileMappings = $this->searchUser->getUserProfileMapping();
        /**
         * Create a dummy object to set the default value to the current user
         *
         * Not doing this in the form as it overcomplicates the form
         */
        $dummyObject = new UserProfileMapping(Doctrine::getTable('UserProfileMapping'), true);
        $dummyObject->setSfGuardUser($this->searchUser);
        $this->addProfileMappingForm = new UserProfileMappingForm($dummyObject);
    }

    public function executeUnlinkUserProfile(sfWebRequest $request) {
        $this->forward404Unless($mappingToDelete = Doctrine::getTable('UserProfileMapping')->find($request->getParameter('uin')));
        $mappingToDelete->delete();

        $this->redirect('profile/userProfileManagement?uin='.$mappingToDelete->getSfGuardUserUin());
    }

    public function executeLinkUserProfile(sfWebRequest $request) {
        $this->form = new UserProfileMappingForm();

        $profileMapping = $this->processForm($request, $this->form);
        
        if ($profileMapping != null) {
            $this->redirect('profile/userProfileManagement?uin='.$profileMapping->getSfGuardUserUin());
        } else {
            $this->forward404();
        }

    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $savedObject = $form->save();
            return $savedObject;

            //$this->redirect('collection/edit?uin='.$collection_item->getUin());
        } else {
            return null;
        }
    }

}
